package com.compoundsystem.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.compoundsystem.admin.pojo.AdminUser;
import com.compoundsystem.admin.result.Result;
import com.compoundsystem.admin.service.AdminService;
import com.compoundsystem.dto.AdminLoginDto;
import com.compoundsystem.mapper.AdminMapper;
import com.compoundsystem.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    //登入逻辑
    @Override
    public Result login(AdminLoginDto adminLoginDto) {
        if (StringUtils.isEmpty(adminLoginDto.getUsername())){
            return new Result(400,"账号不能为空","");
        }
        if (StringUtils.isEmpty(adminLoginDto.getPassword())){
            return new Result(400,"密码不能为空","");
        }
        //通过登录名查询用户
        AdminUser adminUser= adminMapper.selectByName(adminLoginDto.getUsername());
        //比较密码
        if (adminUser!=null&&adminUser.getPassword().equals(adminLoginDto.getPassword())){
            LoginVO loginVO=new LoginVO();
            loginVO.setId(adminUser.getId());
            //这里token直接用一个uuid
            //使用jwt的情况下，会生成一个jwt token,jwt token里会包含用户的信息
            loginVO.setToken(UUID.randomUUID().toString());
            loginVO.setAdminUser(adminUser);
            return new Result(200,"",loginVO);
        }
        return new Result(401,"登录失败","");
    }
}


